import java.util.ArrayList;
import java.util.List;
import java.util.Random;

//扑克牌
public class CardDemo {
    public static final String[] SUITS = {"♠","♥","♣","◆"};
    //买一副牌
    private static List<Card> buyCard(){
        List<Card> cards = new ArrayList<>(52);
        //二维数组存储
        for (int i = 0; i < 4; i++) { //花色
            for (int j = 0; j < 13; j++) { //卡片值
                String suit = SUITS[i];
                int rank = j;

                Card card = new Card();
                card.rank = rank;
                card.suit = suit;

                cards.add(card);
            }
        }
        return cards;
    }

    private static void swap(List<Card> cards, int i, int r){
        //面向对象中获取数组的元素要用get()
        Card t = cards.get(i);
        cards.set(i, cards.get(r));
        cards.set(r, t);
    }

    private static void shuffle(List<Card> cards){
        Random random = new Random(20221102);
        for (int i = cards.size()-1; i > 0; i--){
            int r = random.nextInt(i); //在[0-i)的随机数
            swap(cards, i, r);
        }
    }

    public static void main(String[] args) {
        List<Card> cards = buyCard();
        System.out.println("刚买回来的牌：");
        System.out.println(cards);

        //洗牌
        shuffle(cards);
        System.out.println("洗过的牌：");
        System.out.println(cards);

        //三个人，每个人轮流抓5张牌
        List<List<Card>> person = new ArrayList<>(); //二维数组:三个人是一个集合，每个人对应一个牌的集合
        //实例化三个人的集合
        person.add(new ArrayList<>());
        person.add(new ArrayList<>());
        person.add(new ArrayList<>());

        for (int i = 0; i < 5; i++) { //第几次抓牌
            for (int j = 0; j < 3; j++) { //人循环抓牌
                person.get(j).add(cards.remove(0));
            }
        }
        for (int i = 0; i < person.size(); i++) {
            System.out.println("第"+i+"个人手中的牌："+person.get(i));
        }

        System.out.println("剩余的牌：");
        System.out.println(cards);
    }
}
